package com.hxx.day8;

import org.junit.Test;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Lock锁
 */
public class TestDemo8 {
    @Test
    public void testLock(){
        Lock lock = new ReentrantLock();
        lock. lock();
        try {
            System. out. println("获得锁");
        } catch (Exception e) {
            // TODO: handle exception
        } finally {
            System. out. println("释放锁");
            lock. unlock();
        }
    }
    /**
     * 线程池：固定大小的线程池
     */
     @Test
     public void testThreadPool(){
         ExecutorService executorService = Executors.newFixedThreadPool(5);
         for(int i=0;i<10;i++){
             executorService.submit(()->{
                 System.out.println("线程名称:"+Thread.currentThread().getName());
                 try {
                     Thread.sleep(2000);
                 } catch (InterruptedException e) {
                     e.printStackTrace();
                 }
             });
         }
     }
}






























